import { clipPlayer, clipInit } from "../clip.js";

const { Container } = PIXI;
export class BasePlayer {
  constructor(resource, Info, playerId) {
    this.attackLock = false;
    this.inited = false;
    this.state = "";
    this.playerId = playerId;
    this.container = new Container();
    this.Info = Info;
    this.resource = resource;
    this.performance = {
      alert: null,
      stand1: null,
      jump: null,
      walk1: null,
      swingO1: null,
      stabO1: null,
      stabO2: null,
      rope: null,
      ladder: null
    };
    this.animationSprite = null;
    let _position = {
      x: 1754,
      y: 344.1,
    };

    this.position = new Proxy(_position, {
      set: (target, key, value) => {
        if (key === "x") {
          this.container[key] = Number(value);
        }
        if (key === "y") {
          this.container[key] = Number(value);
        }
        return Reflect.set(target, key, value);
      },
    });
    this.xSpeed = 2;
    this.ySpeed = 2;
    this.gravity = 0.5;
    this.layer = 0;
    this.fhId = 0;
  }
  update() {
    
  }
  async init() {
    clipInit(this.resource, this.Info);

    for (let key in this.performance) {
      this.performance[key] = await clipPlayer(this.resource, this.Info, key);
      this.performance[key].updateAnchor = true;
      this.performance[key].animationSpeed = 0.05;
    }
    this.performance.stabO1.loop = false;
    this.performance.stabO2.loop = false;
    this.performance.swingO1.loop = false;

    this.performance.stabO1.onFrameChange = (num) => {
      if (num === 1) {
      }
    };
    this.performance.stabO1.onComplete = () => {
      this.attackLock = false;
      this.stand1();
    };
    this.performance.stabO2.onFrameChange = (num) => {
      if (num === 1) {
      }
    };
    this.performance.stabO2.onComplete = () => {
      this.attackLock = false;
      this.stand1();
    };

    this.performance.swingO1.onFrameChange = (num) => {
      if (num === 2) {
      }
    };
    this.performance.swingO1.onComplete = () => {
      this.attackLock = false;
      this.stand1();
    };

    this.animationSprite = this.performance.stand1;
    this.container.x = this.position.x;
    this.container.y = this.position.y;
    this.container.addChild(this.animationSprite);
    this.transform("stand1");
  }
  transform(goalAniName) {
    if (this.state === goalAniName) {
      return;
    }
    this.state = goalAniName;
    let nextAni = this.performance[goalAniName];
    let nowAni = this.animationSprite;
    this.container.removeChild(nowAni);
    this.animationSprite = nextAni;
    this.animationSprite.gotoAndPlay(0);
    this.container.addChild(this.animationSprite);
  }
  setLayer(layer, stage) {
    if (this.layer === layer) {
      return;
    }

    this.layer = layer;
    this.container.zIndex = layer;
    stage.removeChild(this.container);
    stage.addChild(this.container);
  }
  stand1() {
    this.transform("stand1");
  }

  jump() {
    this.transform("jump");
  }

  alert() {
    this.transform("alert");
  }

  walk1() {
    this.transform("walk1");
  }

  swingO1() {
    this.transform("swingO1");
    this.attackLock = true;
  }

  stabO2() {
    this.transform("stabO2");
    this.attackLock = true;
  }

  stabO1() {
    this.transform("stabO1");
    this.attackLock = true;
  }
  ladder() {
    this.transform('ladder');
  }
  rope () {
    this.transform('rope');
  }

  turnToRight() {
    if (this.animationSprite.scale.x === 1) {
      // this.animationSprite.x -= (this.animationSprite.width)
    }
    this.container.scale.x = -1;
  }

  turnToLeft() {
    if (this.animationSprite.scale.x === -1) {
      // this.animationSprite.x += (this.animationSprite.width)
    }
    this.container.scale.x = 1;
  }
}
